package com.macro.mall.tiny.modules.pms.controller;


import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.common.service.RedisService;
import com.macro.mall.tiny.modules.pms.service.*;
import com.macro.mall.tiny.modules.ums.model.UmsMember;
import com.macro.mall.tiny.security.util.MemberContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 用户行为表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/pms/pmsUserAction")
@Api(tags = "PmsUserActionController")
@Tag(name = "PmsUserActionController", description = "用户行为")
public class PmsUserActionController {

    public static final String REDIS_KEY_PREFIX = "pms:user:action";

    public static final String REDIS_KEY_DETAIL_PREFIX = "pms:user:action_detail";

    @Autowired
    PmsUserActionService pmsUserActionService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private PmsErrorCaseService pmsErrorCaseService;

    @Autowired
    private PmsErrorCodeService pmsErrorCodeService;

    @Autowired
    private PmsElectricMasterDataService pmsElectricMasterDataService;
    @Autowired
    private PmsRepairDataService pmsRepairDataService;


    @ApiOperation("点赞/取消点赞 ")
    @GetMapping("/like")
    public CommonResult<Void> like(@ApiParam("行为id 0点赞 1取消点赞 2收藏 3取消收藏")
                                   @RequestParam Integer actionId,

                                   @ApiParam("行为目标类型 0错误码1故障现象 2电工大师 3维修数据 4中央数据")
                                   @RequestParam Integer type,

                                   @ApiParam("行为目标主键id")
                                   @RequestParam Integer id) {
        if (MemberContext.getMember() == null) {
            return CommonResult.failed("请先登录");
        }
        if (actionId == 0) {
            Boolean b = redisService.sIsMember(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);
            if (b) {
                return CommonResult.failed("已经点赞过了");
            }
            redisService.getRedisTemplate()
                    .opsForSet().add(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);
            if (type == 0) {
                pmsErrorCodeService.update().setSql("like_num = like_num + 1").eq("id", id).update();
            } else if (type == 1) {
                pmsErrorCaseService.update().setSql("like_num = like_num + 1").eq("id", id).update();
            }
        } else if (actionId == 1) {
            Long removedNum = redisService.getRedisTemplate()
                    .opsForSet().remove(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);

            if (removedNum != null && removedNum > 0) {
                if (type == 0) {
                    pmsErrorCodeService.update().setSql("like_num = like_num - 1").eq("id", id).update();
                } else if (type == 1) {
                    pmsErrorCaseService.update().setSql("like_num = like_num - 1").eq("id", id).update();
                }
            } else {
                return CommonResult.failed("未点赞过");
            }
        } else if (actionId == 2) {
            Boolean b = redisService.sIsMember(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            if (b) {
                return CommonResult.failed("已经收藏过了");
            }
            redisService.getRedisTemplate()
                    .opsForSet().add(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            if (type == 2) {
                pmsElectricMasterDataService.update().setSql("collect_num = collect_num + 1").eq("id", id).update();
            }
            if (type == 3) {
                pmsRepairDataService.update().setSql("collect_num = collect_num + 1").eq("id", id).update();
            }
        } else if (actionId == 3) {
            Boolean b = redisService.sIsMember(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            if (!b) {
                return CommonResult.failed("未收藏过");
            }
            Long removedNum = redisService.getRedisTemplate()
                    .opsForSet().remove(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
            if (removedNum != null && removedNum > 0) {
                if (type == 2) {
                    pmsElectricMasterDataService.update().setSql("collect_num = collect_num - 1").eq("id", id).update();
                }
                if (type == 3) {
                    pmsRepairDataService.update().setSql("collect_num = collect_num - 1").eq("id", id).update();
                }
            } else {
                return CommonResult.failed("未收藏过");
            }

        }
        return CommonResult.success(null);
    }


    @ApiOperation("获取点赞收藏状态")
    @GetMapping("/getLikeCollectStatus")
    public CommonResult<Boolean> getLikeCollectStatus(@ApiParam("行为目标类型 0错误码1故障现象 2电工大师 3维修数据 4中央数据")
                                                               @RequestParam Integer type,
                                                           @ApiParam("行为目标主键id") @RequestParam Integer id,
                                                           @ApiParam("行为id 0点赞 1取消点赞 2收藏 3取消收藏") @RequestParam Integer actionId) {
        if (MemberContext.getMember() == null) {
            return CommonResult.failed("请先登录");
        }
        Boolean flag = false;
        if (actionId == 0) {
            flag = redisService.sIsMember(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":like:" + MemberContext.getMember().getId(), id);
        } else if (actionId == 2) {
            flag = redisService.sIsMember(REDIS_KEY_DETAIL_PREFIX + ":" + type + ":collect:" + MemberContext.getMember().getId(), id);
        }

        return CommonResult.success(flag, "获取成功");
    }


}

